--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/9/14
-- Time: 15:24
-- To change this template use File | Settings | File Templates.
-- 一种冲锋类的士兵

local MotionStreak = cc.MotionStreak
local Sowar = extend(FightUnit)

Sowar.sprint = false    -- 是否在冲刺
Sowar.sprintDistance = 100  -- 在还剩多远的时候冲刺
Sowar.animSprint = nil
Sowar.doneSprint = true   -- 找到一个目标 ， 距离在 大于sprintDistance * 2， 可以进行冲刺
Sowar.baseMoveSpeed = 0

-- 构造一个战斗基本单元
function Sowar:create(battlefield, military, name)
    local unit = xcopy(Sowar)
    unit.id = military:getAllUnitCount() .. "_" .. military:getId()


    unit.name = name
    unit.battlefield = battlefield
    unit.military = military

    unit.moveTime = os.clock()
    unit.attackTime = os.clock()
    unit.faceDirection = FACE_RIGHT


    return unit
end

-- 重写寻找目标
function Sowar:autoFindTarget()
    -- 冲锋不寻找目标
    if self.target ~= nil and not self.target:isValid() and not self.sprint then
        self.target = nil
    end

    -- 得到敌方阵营
    local targetMly = self.military:getTargetMilitary()

    -- 找到一个离我最近的目标
    local targetUnits = targetMly:getKeyUnits()
    local nearestDistance = -1
    if self.target ~= nil then
        return
    end

    -- 寻找目标卡顿,3次离散寻找
    local counter = #targetUnits
    if #targetUnits > 3 then
        counter = 3
    end
    for i = 1, counter do
        local index = math.random(1, #targetUnits)
        local targetUnit = targetUnits[index]
        local distance = distance(self.position, targetUnit.position)
        if (nearestDistance == -1 or distance < nearestDistance) and targetUnit:isValid() then
            nearestDistance = distance
            self.target = targetUnit
            if i == counter then
                break
            end
        end
    end
    local targetName = "nil"
    if self.target ~= nil then
        targetName = self.target.name
        -- 找到目标
        if nearestDistance > self.sprintDistance then
            self.doneSprint = false
            logW(self.name .. " 冲刺目标 " .. targetName)
        end
    end
    logD(self.name .. " 目标 " .. targetName)
end

-- 右冲锋
function Sowar:startSprint()
    logW(self.name .. " Sowar 开始冲刺")
    self.currentAnim = self.anim.moveFrames
    self.sprint = true
    self.baseMoveSpeed = self.moveSpeed
    self.moveSpeed = self.moveSpeed * 2

    -- 刷新动画
    self:refreshCurrentAnimUI()
    self.sprintUi = MotionStreak:create(0.3, 15, 6.0, cc.c3b(186, 186, 186), "streak.png")
    self.battlefield:getSceneNode():addChild(self.sprintUi)

    self:resetZOrder()
end

function Sowar:stopSprint()
    logW(self.name .. " Sowar 冲刺结束")
    self.battlefield:getSceneNode():removeChild(self.sprintUi, true)
    
    self.currentAnim = self.anim.moveFrames
    self.sprint = false
    self.doneSprint = true
    self.moveSpeed = self.baseMoveSpeed

    -- 刷新动画
    self:refreshCurrentAnimUI()
end

function Sowar:resetZOrder()
    -- 设置zorder
    local y = math.floor(self.position.y)

    self.ui:setLocalZOrder(self.battlefield.size.height - y)
    local radio = 1 - (y * 0.3) / self.battlefield.size.height
    logD("reset z order " , y , radio)
    self.ui:setScale(radio * self.faceDirection * self.shape, radio * self.shape)

    if self.sprintUi ~= nil then
        --self.sprintUi:setLocalZOrder(self.battlefield.area.height - y)
        --self.sprintUi:setScale(radio * self.faceDirection * self.shape, radio * self.shape)
    end
end

function Sowar:sprintEffect(bf, c, r)
    logW(self.name .. " 冲刺践踏", c, r)
end

function Sowar:sprintTarget(dt, currentTime, currentIndex)
    local target = self.target
    local av = self.attackValue 
    local targetEnv = self.battlefield:getEnvEffect(target.position)
    target.hp = target.hp - (av / (target.defence *(1 - targetEnv.dsd))+ av / 2)
    logW(self.name .. " 冲刺上海 " .. target.hp)
    if target.hp < 0 then
        target.hp = 0
    end
    
    self:stopSprint()
end

function Sowar:localProcess(dt, currentTime, currentIndex)
    if self.target == nil or not self.target:isValid() then
        if self.sprint == true then
            self:stopSprint()
        end
        return    
    end
    
    local distance = distance(self.position, self.target.position)
    if distance < self.sprintDistance and self.sprint == false and self.doneSprint == false then
        -- 发起冲锋    TODO
        self:startSprint()
    end

    if self.sprint == true then
        self.sprintUi:setPosition(self.position)
    end
end

-- 攻击
function Sowar:attack(target, dt, currentTime, currentIndex)
    -- 如果找到目标，判断是否在攻击范围内
    if self.sprint == true then
        if target ~= nil and target:isValid() then
            self:sprintTarget(dt, currentTime, currentIndex)
            self.attackTime = currentTime
        end
    else
        local envEffect = self.battlefield:getEnvEffect(self.position)
        if currentTime - self.attackTime < self.attackSpeed * (1 + envEffect.asd)then
            return
        end

        if target == nil or not target:isValid() then
            return
        end
        
        self.faceDirection = self:getTargetDirection(target.position)
        self:playAttackUI()
        --target:playHitUI()
        --logW(self.name .. " 攻击 " .. target.name)
        if self.attackMode == FAR_ATTACK then
            self:createAttackUnit(currentTime, currentIndex, envEffect)
        else
            self:attackCalculate(currentTime, currentIndex, envEffect)
        end

        self.attackTime = currentTime
    end
    
    
end

return Sowar

